package com.lonely.fx.bind;

import javafx.beans.property.SimpleMapProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;

/**
 * @date: 2022-07-23 17:33
 * @author: ztkj-hzb
 * @description: 基于List类型的单向绑定
 **/
public class BindWithMapSettingMain {


    public static void main(String[] args) {

        ObservableMap<String, String> observableMap1 = FXCollections.observableHashMap();
        observableMap1.put("name", "aa");
        observableMap1.put("age", "11");
        SimpleMapProperty<String, String> simpleMapProperty1 = new SimpleMapProperty<>(observableMap1);

        ObservableMap<String, String> observableMap2 = FXCollections.observableHashMap();
        observableMap2.put("name", "aa");
        observableMap2.put("age", "11");
        SimpleMapProperty<String, String> simpleMapProperty2 = new SimpleMapProperty<>(observableMap2);

        System.out.println(String.format("simpleMapProperty1: %s , simpleMapProperty2: %s", simpleMapProperty1.get(),
                simpleMapProperty2.get()));
        System.out.println(String.format("observableMap1: %s , observableMap2: %s", observableMap1, observableMap2));


        // simpleMapProperty1 绑定到 simpleMapProperty2 上 , 虽数据共享，但是底层的list还是分开的
//        simpleMapProperty1.bind(simpleMapProperty2);
//
//        simpleMapProperty1.put("address","hb");
//        simpleMapProperty2.put("address2","wh");
//
//        System.out.println(String.format("simpleMapProperty1: %s , simpleMapProperty2: %s", simpleMapProperty1.get(),
//                simpleMapProperty2.get()));
//        System.out.println(String.format("observableMap1: %s , observableMap2: %s", observableMap1, observableMap2));


        // simpleMapProperty2 绑定到 simpleMapProperty1 上 , 对于2的修改会同步到1上
//        simpleMapProperty1.bindContent(simpleMapProperty2);
//
//        simpleMapProperty1.put("address","hb");
//        simpleMapProperty2.put("address2","wh");
//
//        System.out.println(String.format("simpleMapProperty1: %s , simpleMapProperty2: %s", simpleMapProperty1.get(),
//                simpleMapProperty2.get()));
//        System.out.println(String.format("observableMap1: %s , observableMap2: %s", observableMap1, observableMap2));


        // 数据同步，但是底层的map分别维护
//        simpleMapProperty1.bindBidirectional(simpleMapProperty2);
//
//        simpleMapProperty1.put("address","hb");
//        simpleMapProperty2.put("address2","wh");
//
//        System.out.println(String.format("simpleMapProperty1: %s , simpleMapProperty2: %s", simpleMapProperty1.get(),
//                simpleMapProperty2.get()));
//        System.out.println(String.format("observableMap1: %s , observableMap2: %s", observableMap1, observableMap2));


        //真正的数据共享
        simpleMapProperty1.bindContentBidirectional(simpleMapProperty2);

        simpleMapProperty1.put("address", "hb");
        simpleMapProperty2.put("address2", "wh");

        System.out.println(String.format("simpleMapProperty1: %s , simpleMapProperty2: %s", simpleMapProperty1.get(),
                simpleMapProperty2.get()));
        System.out.println(String.format("observableMap1: %s , observableMap2: %s", observableMap1, observableMap2));


    }


}
